﻿/******************************************************/
// BOLNews 事業運営クラス  
/******************************************************/

using System;
using System.Data;
using System.Data.SqlClient;
using log4net;
using MAA.Data;
using MAA.Entity;

namespace MAA.Business
{
    /// <summary>
    /// ニュースを管理するために参照の商務操作を行うこと 
    /// </summary>
    public class BOLNews
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// すべてのお米の種類の情報を表示する 
        /// </summary>
        /// <param name="tblRice"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewRiceInfo(string lang,int pageIndex, int pageSize, out long rowNum, out DataTable tblRice, out string message)
        {
            message = "";
            tblRice = null;
            rowNum = 0;

            DALRice dalRice = new DALRice();
            try
            {
                log.Info("view rice info");

                // Get information of all rice types
                tblRice = dalRice.getRice(lang, pageIndex, pageSize, out rowNum);

                if (tblRice.Rows.Count > 0)
                {
                    // Them cot brief description
                    DataColumn column = new DataColumn();
                    column.ColumnName = "BriefDescription";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblRice.Columns.Add(column);

                    string description = "";
                    for (int i = 0; i < tblRice.Rows.Count; ++i)
                    {
                        description = Convert.ToString(tblRice.Rows[i]["Description"]);
                        if (description.Length > 100)
                        {
                            tblRice.Rows[i]["BriefDescription"] = description.Substring(0, 100) + "...";
                        }
                        else
                        {
                            tblRice.Rows[i]["BriefDescription"] = description;
                        }
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="riceID"></param>
        /// <param name="tblRice"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewRiceInfoRandom(string lang,int riceID, out DataTable tblRice, out string message)
        {
            message = "";
            tblRice = null;

            DALRice dalRice = new DALRice();
            try
            {
                log.Info("view rice info by random");

                // Get information of all rice types
                tblRice = dalRice.getRiceRandom(lang,riceID);

                if (tblRice.Rows.Count > 0)
                {
                    // Them cot brief description
                    DataColumn column = new DataColumn();
                    column.ColumnName = "BriefDescription";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblRice.Columns.Add(column);

                    string description = "";
                    for (int i = 0; i < tblRice.Rows.Count; ++i)
                    {
                        description = Convert.ToString(tblRice.Rows[i]["Description"]);
                        if (description.Length > 100)
                        {
                            tblRice.Rows[i]["BriefDescription"] = description.Substring(0, 100) + "...";
                        }
                        else
                        {
                            tblRice.Rows[i]["BriefDescription"] = description;
                        }
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// お米の種類の詳細を参照の情報 
        /// </summary>
        /// <param name="riceID"></param>
        /// <param name="tblRice"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewRiceInfoDetail(string lang,int riceID, out DataTable tblRice, out string message)
        {
            message = "";
            tblRice = null;

            DALRice dalRice = new DALRice();
            try
            {
                log.Info("view rice info detail");

                // Get detail information of a rice type
                tblRice = dalRice.getRiceInfo(lang,riceID);

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// すべてのお米の種類のビュー名 表示
        /// </summary>
        /// <param name="tblRiceType"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewRiceType(string lang,out DataTable tblRiceType, out string message)
        {
            message = "";
            DALRice dalRice = new DALRice();
            tblRiceType = null;

            try
            {
                log.Info("view rice type");

                /// すべてのお米の種類の名前を取得する
                tblRiceType = dalRice.getRiceType(lang);

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// すべてのお米の種類のビュー市場価格 
        /// </summary>
        /// <param name="tblRicePrice"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewRicePrice(string lang, out DataTable tblRicePrice, out string message)
        {
            message = "";
            tblRicePrice = null;
            DALRice dalRice = new DALRice();

            try
            {
                log.Info("view rice price");

                /// すべてのお米の種類の市場価格を得る
                tblRicePrice = dalRice.getRicePrice(lang);

                /// tblRicePriceに列を追加する
                if (tblRicePrice.Rows.Count > 0)
                {
                    DataColumn column;

                    // 列のステータス：変更の状態の画像
                    column = new DataColumn();
                    column.ColumnName = "Status";
                    column.DataType = typeof(string);
                    tblRicePrice.Columns.Add(column);

                    // 列の変更：米のOldPriceとupdatePriceでの間変化率（％）
                    column = new DataColumn();
                    column.ColumnName = "Change";
                    column.DataType = typeof(string);
                    tblRicePrice.Columns.Add(column);

                    double updatePrice = 0;
                    double oldPrice = 0;
                    for (int i = 0; i < tblRicePrice.Rows.Count; i++)
                    {
                        updatePrice = Convert.ToDouble(tblRicePrice.Rows[i]["UpdatePrice"]);
                        oldPrice = Convert.ToDouble(tblRicePrice.Rows[i]["OldPrice"]);
                        if (updatePrice > oldPrice)
                        {
                            tblRicePrice.Rows[i]["Change"] = "+" + (updatePrice - oldPrice).ToString();
                            tblRicePrice.Rows[i]["Status"] = Common.URL_UP_IMAGE;
                        }
                        if (updatePrice < oldPrice)
                        {
                            tblRicePrice.Rows[i]["Change"] = "-" + (oldPrice - updatePrice).ToString();
                            tblRicePrice.Rows[i]["Status"] = Common.URL_DOWN_IMAGE;
                        }
                        if (updatePrice == oldPrice)
                        {
                            tblRicePrice.Rows[i]["Change"] = "0";
                            tblRicePrice.Rows[i]["Status"] = Common.URL_EQUAL_IMAGE;
                        }
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        ///  
        /// </summary>
        /// <param name="tblNews"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewNewsUpdate(string lang, out DataTable tblNews, out string message)
        {
            message = "";
            tblNews = null;

            DALNews dalNews = new DALNews();
            try
            {
                log.Info("view news update");

                /// Get information of 6 newest market news
                tblNews = dalNews.getNewsUpdate(lang);

                /// Add columns to tblNews
                if (tblNews.Rows.Count > 0)
                {
                    DataColumn column = new DataColumn();

                    // column BriefContent: brief content of news (100 first characters of Content)
                    column.ColumnName = "BriefContent";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblNews.Columns.Add(column);

                    string content = "";
                    for (int i = 0; i < tblNews.Rows.Count; ++i)
                    {
                        content = Convert.ToString(tblNews.Rows[i]["Content"]);
                        if (content.Length > 100)
                        {
                            tblNews.Rows[i]["BriefContent"] = content.Substring(0, 100) + "...";
                        }
                        else
                        {
                            tblNews.Rows[i]["BriefContent"] = content;
                        }
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// すべての市場のニュースの情報を表示する
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowNum"></param>
        /// <param name="tblNews"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewNewsAll(string lang, int pageIndex, int pageSize, out long rowNum, out DataTable tblNews, out string message)
        {
            message = "";
            tblNews = null;
            rowNum = 0;

            DALNews dalNews = new DALNews();
            try
            {
                log.Info("view all news");
                // Get information of all market news
                tblNews = dalNews.getAllNews(lang, pageIndex, pageSize, out rowNum);

                // Check getNews: If DataTable has data
                if (tblNews.Rows.Count > 0)
                {
                    // Add columns to tblNews: column BriefContent: brief content of news (100 first characters of Content)
                    DataColumn column = new DataColumn();
                    column.ColumnName = "BriefContent";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblNews.Columns.Add(column);

                    string content = "";
                    for (int i = 0; i < tblNews.Rows.Count; ++i)
                    {
                        content = Convert.ToString(tblNews.Rows[i]["Content"]);
                        if (content.Length > 100)
                        {
                            tblNews.Rows[i]["BriefContent"] = content.Substring(0, 100) + "...";
                        }
                        else
                        {
                            tblNews.Rows[i]["BriefContent"] = content;
                        }
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newsID"></param>
        /// <param name="tblNews"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewNewsRandom(string lang, int newsID, out DataTable tblNews, out string message)
        {
            message = "";
            tblNews = null;

            DALNews dalNews = new DALNews();
            try
            {
                log.Info("view news by random");
                // Get information of all market news
                tblNews = dalNews.getNewsRandom(lang, newsID);

                // Check getNews: If DataTable has data
                if (tblNews.Rows.Count > 0)
                {
                    // Add columns to tblNews: column BriefContent: brief content of news (100 first characters of Content)
                    DataColumn column = new DataColumn();
                    column.ColumnName = "BriefContent";
                    column.DataType = typeof(string);
                    column.DefaultValue = "";
                    tblNews.Columns.Add(column);

                    string content = "";
                    for (int i = 0; i < tblNews.Rows.Count; ++i)
                    {
                        content = Convert.ToString(tblNews.Rows[i]["Content"]);
                        if (content.Length > 100)
                        {
                            tblNews.Rows[i]["BriefContent"] = content.Substring(0, 100) + "...";
                        }
                        else
                        {
                            tblNews.Rows[i]["BriefContent"] = content;
                        }
                    }
                }

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 市場ニュースの詳細を参照の情報 
        /// </summary>
        /// <param name="newsID"></param>
        /// <param name="tblNews"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewNewsDetail(string lang, int newsID, out DataTable tblNews, out string message)
        {
            message = "";
            tblNews = null;

            DALNews dalNews = new DALNews();
            try
            {
                log.Info("view news detail");
                // Get detail information of a market news
                tblNews = dalNews.getNewsInfo(lang, newsID);

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// すべての州のビュー名表示
        /// </summary>
        /// <param name="tblProvince"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool viewProvince(string lang, out DataTable tblProvince, out string message)
        {
            message = "";
            tblProvince = null;
            DALNews news = new DALNews();

            try
            {
                log.Info("view province");

                /// すべての州の名前を取得する
                tblProvince = news.getProvince(lang);

                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }
    }
}